﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WebCamDeskLib;
using System.Runtime.InteropServices;

namespace WCDTray
{
    enum wcdDisplayModes {Single, Random, Series};
    public partial class FrmWCDMain : Form
    {

        private const int SPI_SETDESKWALLPAPER = 0X14;
        private const int SPIF_UPDATEINIFILE = 0X1;
        private const int SPIF_SENDWININICHANGE = 0X2;

        [DllImport("USER32.DLL", EntryPoint = "SystemParametersInfo", SetLastError = true)]
        private static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);

        
        private string fFileName;
        private WebCamList fWebCamList;
        private wcdDisplayModes fDisplayMode = wcdDisplayModes.Series;
        private Boolean fCanLoadPicture = true;
        private WebCam fWebCam;
        

        public FrmWCDMain()
        {
            InitializeComponent();
        }

        private void FrmWCDMain_Resize(object sender, EventArgs e)
        {
            if (FormWindowState.Minimized == WindowState)
                Hide();
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            Show();
            WindowState = FormWindowState.Normal;
        }

        private void optionenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Show();
            WindowState = FormWindowState.Normal;
        }

        private void beendenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void FrmWCDMain_Load(object sender, EventArgs e)
        {
            fFileName = String.Format(@"{0}\MyWebCamDeskPic.jpg", Environment.GetFolderPath(Environment.SpecialFolder.MyPictures));
            edFileName.Text = fFileName;

            fWebCamList = new WebCamList("setup.ini");
            GetPicture();
            timer1.Enabled = true;

                        
        }

        private void GetPicture()
        {
            if (fCanLoadPicture)
            {
                fCanLoadPicture = false;
                
                if (fDisplayMode == wcdDisplayModes.Random)
                    fWebCam = fWebCamList.GetRandomWebCam();
                if (fDisplayMode == wcdDisplayModes.Series)
                    fWebCam = fWebCamList.GetNextWebCam();
                if (fDisplayMode == wcdDisplayModes.Single)
                {
                    if (fWebCam == null)
                        fWebCam = fWebCamList.GetRandomWebCam();
                }

                Downloader myDownloader = new Downloader();
                myDownloader.RaiseDownloadCompleteEvent += myDownloader_RaiseDownloadCompleteEvent;
                myDownloader.DownloadFile(fWebCam.Adress, fFileName);

                notifyIcon1.BalloonTipText = String.Format("{0}\n{1}, {2}", fWebCam.Adress, fWebCam.PicSize, System.DateTime.Now.ToShortTimeString());
                notifyIcon1.BalloonTipTitle = fWebCam.Description;
                notifyIcon1.Text = fWebCam.Description;
                notifyIcon1.ShowBalloonTip(5);
            }
        }

        void myDownloader_RaiseDownloadCompleteEvent(object sender, DownloadEventArgs e)
        {
            fCanLoadPicture = true;
            SetBackground();
        }

        private void SetBackground()
        {
            SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, fFileName, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
        
        
        
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            GetPicture();
            
        }

        private void toolStripComboBox1_DropDownClosed(object sender, EventArgs e)
        {
            if (toolStripComboBox1.SelectedItem.ToString() == "Zufall")
                fDisplayMode = wcdDisplayModes.Random;
            if (toolStripComboBox1.SelectedItem.ToString() == "Serie")
                fDisplayMode = wcdDisplayModes.Series;
            if (toolStripComboBox1.SelectedItem.ToString() == "Refresh")
                fDisplayMode = wcdDisplayModes.Single;
        }

        private void toolStripMenuItem1_CheckStateChanged(object sender, EventArgs e)
        {
            timer1.Enabled = (toolStripMenuItem1.CheckState == CheckState.Checked);
        }
    }
}
